﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;

namespace Universum.Infrastructure.Utils.Extensions
{
	/// <summary>
	/// Regexes for different expressions
	/// </summary>
	public class RegularExpressions : IRegularExpressions
	{
		
		private Dictionary<string, Regex> expressions;

		public RegularExpressions()
		{
			

			loadExpressions();
		}

		#region IRegularExpressions Members

		public Regex GetExpression(string expressionName)
		{
			return expressions[expressionName];
		}

		public bool IsMatch(string expressionName, string input)
		{
			Regex expression = expressions[expressionName];

			return expression.IsMatch(input);
		}

		public string Clean(string expressionName, string input)
		{
			Regex expression = expressions[expressionName];

			return expression.Replace(input, "");
		}

		#endregion
		public const string IS_SLUG = "IsSlug";
		public const string SLUG_REPLACE = "SlugReplace";
		public const string IS_TAG = "IsTag";
		public const string TAG_REPLACE = "TagReplace";
		public const string IS_EMAIL = "IsEmail";
		public const string IS_URL = "IsUrl";
		public const string AREA_NAME = "AreaName";
		public const string USERNAME = "CurrentUsername";
		protected void loadExpressions()
		{
			expressions = new Dictionary<string, Regex>(7)
            {
                {
                    IS_SLUG,
                    new Regex(
                          "^[a-z0-9-_]+$"
                            ,
                        RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Multiline
                        )
                },
                {
                    SLUG_REPLACE,
                    new Regex(
                       
                            "([^a-z0-9-_]?)"
                            ,
                        RegexOptions.IgnoreCase | RegexOptions.Compiled
                        )
                },
                {
					IS_TAG
                   ,
                    new Regex(
                       
                            "^[a-z0-9-_ ]+$"
                           ,
                        RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Multiline
                        )
                },
                {
                    TAG_REPLACE,
                    new Regex(
                       
                            "([^a-z0-9]?)"
                            ,
                        RegexOptions.IgnoreCase | RegexOptions.Compiled
                        )
                },
                {
                   IS_EMAIL,
                    new Regex(
                        
                            @"^[a-z0-9]+([-+\.]*[a-z0-9]+)*@[a-z0-9]+([-\.][a-z0-9]+)*$"
                            ,
                        RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Multiline
                        )
                },
                {
                    IS_URL,
                    new Regex(
                       
                            "^https?://(?:[^./\\s'\"<)\\]]+\\.)+[^./\\s'\"<\")\\]]+(?:/[^'\"<]*)*$"
                            ,
                        RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Multiline
                        )
                },
                {
                    AREA_NAME,
                    new Regex(
                       
                            "[^a-z0-9]"
                            ,
                        RegexOptions.Compiled
                        )
                },
                {
                   USERNAME,
                    new Regex(
                        
                            "[a-zA-Z0-9_]+"
                            ,
                        RegexOptions.Compiled
                        )
                }
            };
		}
	}
}
